package com.scf.fruit.business.commodity.bo;

import com.github.pagehelper.PageHelper;
import com.scf.fruit.business.commodity.po.CommodityReq;
import com.scf.fruit.commodity.CommodityDto;
import com.scf.fruit.commodity.CommodityService;
import com.scf.fruit.enums.ExceptionCodeEnum;
import com.scf.fruit.enums.StatusEnum;
import com.scf.fruit.shiro.AdminUserUtil;
import com.scf.fruit.util.ResponseVo;
import com.scf.fruit.util.SpringCglibBeanUtils;
import jdk.net.SocketFlow;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : 孙超飞
 * @title :
 * @description :
 * @copyright :
 * @date : 2018-05-21 18:06
 */
@Component
public class CommodityBo {
    @Resource
    private CommodityService commodityService;

    public ResponseVo queryCommodityByPage(Integer pageNum,Integer length){
        ResponseVo res=new ResponseVo();
        PageHelper.startPage(pageNum,length,false);
        List<CommodityDto> commodityDtos = commodityService.queryCommodityByPage();
        res.setData(commodityDtos);
        return res;
    }

    public ResponseVo insertCommodity(CommodityReq req){
        ResponseVo res=new ResponseVo();
        CommodityDto dto= SpringCglibBeanUtils.convert(req,CommodityDto.class);
        dto.setCreateUserId(AdminUserUtil.getCurrentUserID());
        if(!commodityService.insertCommodity(dto)){
            res.setCode(ExceptionCodeEnum.EX_COMMODITY_ERROR.getValue());
            res.setMsg(ExceptionCodeEnum.EX_COMMODITY_ERROR.getMsg());
        }
        return res;
    }

    public ResponseVo updateCommodity(CommodityReq req){
        ResponseVo res=new ResponseVo();
        CommodityDto dto= SpringCglibBeanUtils.convert(req,CommodityDto.class);
        dto.setUpdateUserId(AdminUserUtil.getCurrentUserID());
        if(!commodityService.updateCommodity(dto)){
            res.setCode(ExceptionCodeEnum.EX_COMMODITY_ERROR.getValue());
            res.setMsg(ExceptionCodeEnum.EX_COMMODITY_ERROR.getMsg());
        }
        return res;
    }


    public ResponseVo deleteCommodity(Long id){
        ResponseVo res=new ResponseVo();
        CommodityDto dto=new CommodityDto();
        dto.setUpdateUserId(AdminUserUtil.getCurrentUserID());
        dto.setDeleteFlag(StatusEnum.NO.getValue());
        dto.setId(id);
        if(!commodityService.deleteCommodity(dto)){
            res.setCode(ExceptionCodeEnum.EX_COMMODITY_ERROR.getValue());
            res.setMsg(ExceptionCodeEnum.EX_COMMODITY_ERROR.getMsg());
        }
        return res;
    }
}
